package com.ehotting.edsta.system.service.impl;

import com.ehotting.eaf.core.utils.DateUtils;
import com.ehotting.edsta.system.domain.dto.AccessCountDto;
import com.ehotting.edsta.system.domain.dto.OperCountDto;
import com.ehotting.edsta.system.domain.vo.AccessCountVo;
import com.ehotting.edsta.system.mapper.HomeMapper;
import com.ehotting.edsta.system.service.IHomeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class HomeServiceImpl implements IHomeService {

    @Resource
    private HomeMapper homeMapper;

    /**
     * 按时间类型统计登录次数
     *
     * @param timeType 时间标志  0:过去7天, 1:过去30天, 2:过去365天
     * @return 在当前时间段的登录次数
     */
    @Override
    public AccessCountVo accessCount(int timeType) {
        Date nowDate = DateUtils.getNowDate();
        int past = 7;
        int xParam = 5;
        switch (timeType){
            case 1:
                past = 30;
                break;
            case 2:
                past = 365;
                xParam = 2;
                break;
            default:
        }
        String startTime = getPastDate(past, nowDate, Calendar.DATE) + " 00:00:00";
        String endTime = getPastDate(-1, nowDate, Calendar.DATE) + " 23:59:59";// 昨天
        List<AccessCountDto> dbResultMap;

        ArrayList<String> pastDateList = getPastDateList(past, nowDate, xParam);
        if (timeType == 2){
            pastDateList = getPastDateList(11,nowDate,xParam);
            dbResultMap = homeMapper.accessCountByMonth(startTime, endTime);
        }else {
            dbResultMap = homeMapper.accessCountByDay(startTime, endTime);
        }
        List<Object[]> resList = new ArrayList<>(pastDateList.size());

        for (int i = 0; i < pastDateList.size(); i++) {
            String key = pastDateList.get(i);
            AccessCountDto dto = dbResultMap.stream().filter(accessCountDto -> accessCountDto.getTime().equals(key))
                    .findFirst().orElse(null);
            resList.add(new Object[]{key, dto == null ? 0 : dto.getNum()});
        }
        AccessCountVo accessCountVo = new AccessCountVo();
        accessCountVo.setSeriesData(resList);
        return accessCountVo;
    }

    /**
     * 按时间类型统计功能操作次数
     *
     * @param timeType 时间标志  0:过去7天, 1:过去30天, 2:过去365天
     * @return 在当前时间段的功能操作次数
     */
    public List<OperCountDto> operCount(int timeType) {
        Date nowDate = DateUtils.getNowDate();
        int past = 7;
        int xParam = 5;
        switch (timeType){
            case 1:
                past = 30;
                break;
            case 2:
                past = 365;
                xParam = 2;
                break;
            default:
        }
        String startTime = getPastDate(past, nowDate, Calendar.DATE) + " 00:00:00";
        String endTime = getPastDate(-1, nowDate, Calendar.DATE) + " 23:59:59";// 昨天
        List<OperCountDto> dbResultMap;

        if (timeType == 2){
            dbResultMap = homeMapper.operCountByMonth(startTime, endTime);
        }else {
            dbResultMap = homeMapper.operCountByDay(startTime, endTime);
        }
        return dbResultMap;
    }

    /**
     * 获取过去n个单位的时间数组
     * @param past int类型n 表示过去n
     * @param date 时间点
     * @param x 5: date, 2: month, 1: year (Calendar类下静态常量)
     * @return  日期数组
     */
    public ArrayList<String> getPastDateList(int past, Date date, int x){
        ArrayList<String> pastDaysList = new ArrayList<>();
        for (int i = past; i >= 0; i--) {
            pastDaysList.add(getPastDate(i, date, x));
        }
        return pastDaysList;
    }

    /**
     * 获取过去第n个单位的日期
     *
     * @param past 前n
     * @param x 5: date, 2: month, 1: year (Calendar类下静态常量)
     * @return
     */
    public static String getPastDate(int past, Date date, int x) {
        String pattern;
        switch (x){
            case 2:
                pattern = "yyyy-MM";
                date = DateUtils.getBeginDayOfMonth();
                break;
            case 1:
                pattern = "yyyy";
                date = DateUtils.getBeginDayOfYear();
                break;
            default:
                pattern = "yyyy-MM-dd";
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(x, calendar.get(x) - past);
        Date today = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(today);
    }
}
